package com.zzy.edu.controller;


import com.zzy.commonutils.R;
import com.zzy.edu.client.VodClient;
import com.zzy.edu.entity.EduVideo;
import com.zzy.edu.service.EduVideoService;
import com.zzy.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author KAZU
 * @since 2023-01-13
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
	
	@Autowired
	private EduVideoService videoService;
	
	@Autowired
	private VodClient vodClient;
	
	/**
	 * 添加小节
	 * */
	@PostMapping("addVideo")
	public R addVideo(@RequestBody EduVideo eduVideo) {
		videoService.save(eduVideo);
		return R.ok();
	}
	
	/**
	 * 删除小节,视频也删除
	 * */
	@DeleteMapping("{id}")
	public R deleteVideo(@PathVariable String id) {
		// 根据小节id得到视频id
		EduVideo eduVideo = videoService.getById(id);
		String videoSourceId = eduVideo.getVideoSourceId();
		if (!StringUtils.isEmpty(videoSourceId)) {
			R result = vodClient.removeAlyVideo(videoSourceId);
			
			if (result.getCode() != 20000) {
				throw new GuliException(20001, "删除失败，触发熔断器");
			}
			
		}
		videoService.removeById(id);
		return R.ok();
	}
	
	/**
	 * 修改小节
	 * */
	@PostMapping("updateVideo")
	public R updateVideo(@RequestBody EduVideo eduVideo) {
		videoService.updateById(eduVideo);
		return R.ok();
	}
	
	/**
	 * byId查询小节
	 * */
	@GetMapping("getVideo/{id}")
	public R getVideo(@PathVariable String id) {
		EduVideo eduVideo = videoService.getById(id);
		return R.ok().data("video",eduVideo);
	}
	
}

